package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.dto.CateDto;
import com.itheima.ydd.dto.UserDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Morty
 * @create 2022 10 13 13:51
 */
@RestController
@RequestMapping("/index")
@CrossOrigin
@Slf4j
public class SysIndexController {
    @Autowired
    private IYddCateService cateService;
    @Autowired
    private IYddArticleService articleService;
    @Autowired
    private IYddAdvposService advposService;
    @Autowired
    private IYddAdvimgService advimgService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IYddUserService userService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IYddTagService yddTagService;
    @Autowired
    private IYddLinkService yddLinkService;

    @GetMapping("/baseInfo")
    public ResultData baseInfo(){
        Map<String,Object> data = new HashMap<>();
        List<YddCate> cates = cateService.list();
        List<CateDto> cateDtos = new ArrayList<>();
        for (YddCate cate : cates) {
            CateDto cateDto = new CateDto();
            BeanUtils.copyProperties(cate,cateDto);
            Integer cateId = cate.getId();
            LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            yddArticleLambdaQueryWrapper.eq(YddArticle::getCateid,cateId);
            List<YddArticle> yddArticles = articleService.list(yddArticleLambdaQueryWrapper);
            int num = yddArticles.size();
            cateDto.setNum(num);
            cateDtos.add(cateDto);
        }
        Integer admin_num = sysUserService.count();
        data.put("admin_num",admin_num);
        data.put("all_cate",cateDtos);
        Integer advpos_num = advposService.count();
        data.put("advpos_num",advpos_num);
        Integer article_num = articleService.count();
        data.put("article_num",article_num);
        Integer cate_num = cateService.count();
        data.put("cate_num",cate_num);
        Integer advimg_num = advimgService.count();
        data.put("advimg_num",advimg_num);

        LambdaQueryWrapper<YddUser> userDataLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userDataLambdaQueryWrapper.last("limit 7");
        List<YddUser> user = userService.list(userDataLambdaQueryWrapper);
        data.put("new_user",user);

        LambdaQueryWrapper<YddArticle> articleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        articleLambdaQueryWrapper.eq(YddArticle::getIshot,1);
        List<YddArticle> yddArticles = articleService.list(articleLambdaQueryWrapper);
        data.put("hot_article",yddArticles);

        LambdaQueryWrapper<YddUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Integer total = userService.count();
        userLambdaQueryWrapper.eq(YddUser::getSex,0);
        Integer user_man = userService.count(userLambdaQueryWrapper);
        Integer user_woman = total- user_man;
        UserDto userDto = new UserDto();
        userDto.setTotal(total);
        userDto.setUser_man(user_man);
        userDto.setUser_woman(user_woman);

        data.put("user",userDto);


        return ResultData.ok("",data);
    }

    @GetMapping("/new")
    public ResultData newData(int page, int limit) {

        Map<String, Object> data = new HashMap<>();
        data.put("desc", "首页大列表");

        //现在的时间
        Date now = new Date();
        //一周前的时间
        Date weekAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7*100);
        //一天前的时间
        Date dayAgo = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);

        //获取一周内新的文章数
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(YddArticle::getCreateDate, weekAgo, now);
        wrapper.eq(YddArticle::getStatus, 2);
        int numWeek = articleService.count(wrapper);
        data.put("num_week", numWeek);

        //获取一天内新的文章数
        LambdaQueryWrapper<YddArticle> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.between(YddArticle::getCreateDate, dayAgo, now);
        wrapper2.eq(YddArticle::getStatus, 2);
        int numDay = articleService.count(wrapper2);
        data.put("num_day", numDay);

        //获取置顶文章
        Page<YddArticle> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.orderByDesc(YddArticle::getCreateDate);
        wrapper3.eq(YddArticle::getIstop, 1);
        wrapper3.eq(YddArticle::getStatus, 2);
        articleService.page(pageInfo, wrapper3);
        List<YddArticle> records = pageInfo.getRecords();

        Page<ArticleDto> articleDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, articleDtoPage, "records");
        List<ArticleDto> yddArticleDtos = records.stream().map(item -> {
            ArticleDto yddArticleDto = new ArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            LambdaQueryWrapper<YddCate> wrapper4 = new LambdaQueryWrapper<>();
            wrapper4.eq(YddCate::getId, item.getCateid());
            YddCate yddCate = cateService.getOne(wrapper4);
            if (yddCate != null) {
                yddArticleDto.setCatename(yddCate.getCatename());
            }
            return yddArticleDto;
        }).collect(Collectors.toList());

        articleDtoPage.setRecords(yddArticleDtos);
        Map<String,Object> map = new HashMap<>();
        map.put("count",articleDtoPage.getTotal());
        map.put("totalPages",articleDtoPage.getTotal()/articleDtoPage.getSize());
        map.put("pageSize",articleDtoPage.getSize());
        map.put("currentPage",articleDtoPage.getCurrent());
        map.put("data",articleDtoPage.getRecords());

        data.put("list", map);

        return ResultData.ok("",data);
    }

    @GetMapping("/index")
    //需要返回数据allCate,config,allTag,allLink,banner,sideBanner,showBanner
    public ResultData index(){
        Map<String, Object> data = new HashMap<>();
        //allCate
        List<YddCate> cates = cateService.list();
        data.put("allCate",cates);

        //config
        //list
        Map<String,Object> configMap = new HashMap<>();
        List<Map<String,Object>> list = new ArrayList<>();
        String[] groupValues1 = new String[]{"站点配置","底部信息","其他配置"};
        Map<String, Object> listMap1 = sysConfigService.getList1("基础配置", "base", groupValues1);
        list.add(listMap1);
        String[] groupValues2 = new String[]{"管理系统配置"};
        Map<String, Object> listMap2 = sysConfigService.getList1("系统设置", "system", groupValues2);
        list.add(listMap2);
        String[] groupValues3= new String[]{"通知公告","前台图片"};
        Map<String, Object> listMap3 = sysConfigService.getList1("其他设置", "custom", groupValues3);
        list.add(listMap3);
        //allData
        List<SysConfig> configs = sysConfigService.list();
        for (SysConfig config : configs) {
            if (config.getId()==125){
                List<Map<String, Object>> sysConfig125 = new ArrayList<>();
                Map<String, Object> sysConfig125Map1 = new HashMap<>();
                sysConfig125Map1.put("label", "是");
                sysConfig125Map1.put("value", "1");
                Map<String, Object> sysConfig125Map2 = new HashMap<>();
                sysConfig125Map2.put("label", "否");
                sysConfig125Map2.put("value", "2");
                sysConfig125.add(sysConfig125Map1);
                sysConfig125.add(sysConfig125Map2);

                config.setConfigValue(sysConfig125);
            }
        }
        configMap.put("list",list);
        configMap.put("allData",configs);
        data.put("config", configMap);

        //allTag
        List<YddTag> yddTags = yddTagService.list();
        data.put("allTag", yddTags);

        //allLink
        List<YddLink> yddLinks = yddLinkService.list();
        data.put("allLink", yddLinks);

        //banner
        LambdaQueryWrapper<YddAdvimg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvimg::getAdvimgpos, 15);
        List<YddAdvimg> yddAdvimgs = advimgService.list(wrapper);
        data.put("banner", yddAdvimgs);

        //sideBanner
        LambdaQueryWrapper<YddAdvimg> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(YddAdvimg::getAdvimgpos, 14);
        List<YddAdvimg> yddAdvimgsSide = advimgService.list(wrapper2);
        data.put("sideBanner", yddAdvimgsSide);

        //showBanner
        LambdaQueryWrapper<YddAdvimg> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.ne(YddAdvimg::getAdvimgpos, 14);
        wrapper3.ne(YddAdvimg::getAdvimgpos, 15);

        List<YddAdvimg> yddAdvimgsShow = advimgService.list(wrapper3);
        data.put("showBanner", yddAdvimgsShow);

        return ResultData.ok("",data);





        /*Map<String,Object> listData = new HashMap<>();

        Map<String, Object> configData1 = new HashMap<>();
        LambdaQueryWrapper<SysConfig> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SysConfig::getGroupValue,"站点配置");
        List<SysConfig> data1 = sysConfigService.list(wrapper1);
        configData1.put("name","站点配置");
        configData1.put("data",data1);

        Map<String, Object> configData2 = new HashMap<>();
        LambdaQueryWrapper<SysConfig> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SysConfig::getGroupValue,"底部信息");
        List<SysConfig> data2 = sysConfigService.list(wrapper2);
        configData2.put("name","底部信息");
        configData2.put("data",data2);

        Map<String, Object> configData3 = new HashMap<>();
        LambdaQueryWrapper<SysConfig> wrapper3 = new LambdaQueryWrapper<>();
        wrapper3.eq(SysConfig::getGroupValue,"其他配置");
        List<SysConfig> data3 = sysConfigService.list(wrapper3);
        configData3.put("name","其他配置");
        configData3.put("data",data3);

        List<Map<String,Object>> children1 = new ArrayList<>();
        children1.add(configData1);
        children1.add(configData2);
        children1.add(configData3);

        listData.put("name","基础设置");
        listData.put("key","base");
        listData.put("childen",children1);*/
    }
    @GetMapping("/hot")
    public ResultData getHot() {

        Map<String, Object> data = new HashMap<>();
        data.put("desc", "侧边最新文章");

        //获取最新文章
        Page<YddArticle> pageInfo = new Page<>(0, 8);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(YddArticle::getLovenum);
        wrapper.orderByDesc(YddArticle::getId);
        wrapper.eq(YddArticle::getStatus, 2);

        articleService.page(pageInfo, wrapper);

        List<YddArticle> records = pageInfo.getRecords();

        List<ArticleDto> yddArticleDtos = records.stream().map(item -> {
            ArticleDto yddArticleDto = new ArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            LambdaQueryWrapper<YddCate> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddCate::getId, item.getCateid());
            YddCate yddCate = cateService.getOne(wrapper1);
            if (yddCate != null) {
                yddArticleDto.setCatename(yddCate.getCatename());
            }
            return yddArticleDto;
        }).collect(Collectors.toList());

        data.put("list", yddArticleDtos);

        return ResultData.ok("",data);

    }

    @GetMapping("/recommend")
    public ResultData recommend() {


        Map<String, Object> data = new HashMap<>();
        data.put("desc", "侧边推荐文章");

        //获取推荐文章
        Page<YddArticle> pageInfo = new Page<>(0, 6);
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(YddArticle::getId);
        wrapper.eq(YddArticle::getIshot, 1);
        wrapper.eq(YddArticle::getStatus, 2);
        List<YddArticle> yddArticles = articleService.list(wrapper);

        List<YddArticle> newArticles = new ArrayList<>();

        for (int i = 0; i < 6; i++) {
            newArticles.add(yddArticles.get(i));
        }

        List<ArticleDto> yddArticleDtos = newArticles.stream().map(item -> {
            ArticleDto yddArticleDto = new ArticleDto();
            BeanUtils.copyProperties(item, yddArticleDto);
            LambdaQueryWrapper<YddCate> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddCate::getId, item.getCateid());
            YddCate yddCate = cateService.getOne(wrapper1);
            if (yddCate != null) {
                yddArticleDto.setCatename(yddCate.getCatename());
            }
            return yddArticleDto;
        }).collect(Collectors.toList());

        data.put("list", yddArticleDtos);

        return ResultData.ok("",data);


    }
}
